<!DOCTYPE html>
<html>
<head>
    <title>网络客户端配置 - RK3588</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #2c3e50;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        select, input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            padding: 10px 15px;
            background: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
            transition: background 0.3s;
        }
        button:hover {
            background: #2980b9;
        }
        button:disabled {
            background: #95a5a6;
            cursor: not-allowed;
        }
        button.danger {
            background: #e74c3c;
        }
        button.danger:hover {
            background: #c0392b;
        }
        .status-container {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
        }
        .status {
            font-weight: bold;
            margin-bottom: 10px;
        }
        .connected {
            background: #d4edda;
            color: #155724;
        }
        .disconnected {
            background: #f8d7da;
            color: #721c24;
        }
        .details {
            margin-top: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 4px;
            font-family: monospace;
        }
        .detail-row {
            display: flex;
            margin-bottom: 5px;
        }
        .detail-label {
            font-weight: bold;
            min-width: 120px;
        }
        .log-container {
            margin-top: 20px;
            max-height: 200px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 10px;
            background: #f8f9fa;
            font-family: monospace;
            font-size: 14px;
        }
        .log-entry {
            margin-bottom: 5px;
            padding-bottom: 5px;
            border-bottom: 1px solid #eee;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>网络客户端配置</h1>
        
        <div class="form-group">
            <label for="protocol">协议类型:</label>
            <select id="protocol">
                <option value="tcp">TCP</option>
                <option value="udp">UDP</option>
            </select>
        </div>
        
        <div class="form-group">
            <label for="ip">目标IP地址:</label>
            <input type="text" id="ip" placeholder="例如: 192.168.1.100" value="192.168.1.100">
        </div>
        
        <div class="form-group">
            <label for="port">目标端口:</label>
            <input type="number" id="port" placeholder="例如: 8080" value="8080">
        </div>
        
        <div>
            <button id="connectBtn">连接</button>
            <button id="disconnectBtn" class="danger">断开</button>
        </div>
        
        <div id="statusContainer" class="status-container disconnected">
            <div id="status" class="status">状态: 未连接</div>
            <div id="details" class="details">
                <div class="detail-row">
                    <span class="detail-label">协议:</span>
                    <span id="detailProtocol">-</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">目标地址:</span>
                    <span id="detailAddress">-</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">重连线程:</span>
                    <span id="detailThread">-</span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">本地IP:</span>
                    <span id="detailLocalIp">-</span>
                </div>
            </div>
        </div>
        
        <div class="form-group">
            <label for="message">发送测试消息:</label>
            <input type="text" id="message" placeholder="输入要发送的消息">
            <button id="sendBtn" style="margin-top: 10px;">发送</button>
        </div>
        
        <h3>系统日志</h3>
        <div id="logContainer" class="log-container"></div>
    </div>

    <script>
        // DOM元素
        const protocolSelect = document.getElementById('protocol');
        const ipInput = document.getElementById('ip');
        const portInput = document.getElementById('port');
        const connectBtn = document.getElementById('connectBtn');
        const disconnectBtn = document.getElementById('disconnectBtn');
        const sendBtn = document.getElementById('sendBtn');
        const messageInput = document.getElementById('message');
        const statusContainer = document.getElementById('statusContainer');
        const statusElement = document.getElementById('status');
        const logContainer = document.getElementById('logContainer');
        
        // 添加日志条目
        function addLogEntry(message, type = 'info') {
            const entry = document.createElement('div');
            entry.className = `log-entry log-${type}`;
            entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
            logContainer.appendChild(entry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }
        
        // 连接按钮点击事件
        connectBtn.addEventListener('click', function() {
            const protocol = protocolSelect.value;
            const ip = ipInput.value.trim();
            const port = portInput.value.trim();
            
            if (!ip || !port) {
                addLogEntry('错误: 请填写完整的IP和端口', 'error');
                alert('请填写完整的IP和端口');
                return;
            }
            
            addLogEntry(`正在配置: ${protocol.toUpperCase()} ${ip}:${port}`);
            
            fetch('/configure', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ protocol, ip, port })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    addLogEntry('配置成功，正在尝试连接...');
                } else {
                    addLogEntry(`配置失败: ${data.message || '未知错误'}`, 'error');
                    alert(`配置失败: ${data.message || '未知错误'}`);
                }
            })
            .catch(error => {
                addLogEntry(`请求失败: ${error}`, 'error');
                alert(`请求失败: ${error}`);
            });
        });
        
        // 断开按钮点击事件
        disconnectBtn.addEventListener('click', function() {
            addLogEntry('正在断开连接...');
            
            fetch('/disconnect', {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    addLogEntry('已断开连接');
                } else {
                    addLogEntry(`断开连接失败: ${data.message || '未知错误'}`, 'error');
                }
            })
            .catch(error => {
                addLogEntry(`断开连接请求失败: ${error}`, 'error');
            });
        });
        
        // 发送按钮点击事件
        sendBtn.addEventListener('click', function() {
            const message = messageInput.value.trim();
            if (!message) {
                addLogEntry('错误: 消息内容不能为空', 'error');
                return;
            }
            
            addLogEntry(`正在发送消息: "${message}"`);
            
            fetch('/send', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ message })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    addLogEntry('消息发送成功');
                } else {
                    addLogEntry(`消息发送失败: ${data.message || '未知错误'}`, 'error');
                }
            })
            .catch(error => {
                addLogEntry(`发送请求失败: ${error}`, 'error');
            });
        });
        
        // 更新状态信息
        function updateStatus() {
            fetch('/status')
                .then(response => response.json())
                .then(data => {
                    // 更新状态显示
                    if (data.connected) {
                        statusContainer.className = 'status-container connected';
                        statusElement.textContent = `状态: 已连接 (${data.protocol.toUpperCase()} ${data.ip}:${data.port})`;
                    } else {
                        statusContainer.className = 'status-container disconnected';
                        statusElement.textContent = '状态: 未连接';
                    }
                    
                    // 更新详细信息
                    document.getElementById('detailProtocol').textContent = data.protocol.toUpperCase();
                    document.getElementById('detailAddress').textContent = data.target_valid ? `${data.ip}:${data.port}` : '未配置';
                    document.getElementById('detailThread').textContent = data.thread_alive ? '运行中' : '未运行';
                    document.getElementById('detailLocalIp').textContent = data.local_ip || '-';
                })
                .catch(error => {
                    addLogEntry(`获取状态失败: ${error}`, 'error');
                });
        }
        
        // 初始化
        addLogEntry('系统初始化完成');
        
        // 每2秒更新一次状态
        setInterval(updateStatus, 2000);
        updateStatus();
    </script>
</body>
</html>